package com.qth.valet.controller;

import com.qth.valet.common.annotation.Logs;
import com.qth.valet.common.utils.ResponseResult;
import com.qth.valet.common.utils.StatusConstants;
import com.qth.valet.common.rouyi.DjApiProperties;
import com.qth.valet.common.rouyi.FileUploadUtils;
import com.qth.valet.common.rouyi.SpringUtils;
import com.qth.valet.entity.po.Orders;
import com.qth.valet.service.OrdersService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
@RequestMapping("/qth/record")
public class RecordController {

    private final OrdersService ordersService;

    /**
     * 通用上传请求（单个）
     */
    @Logs(operation = "上传音频")
    @PostMapping("/upload/{id}")
    public ResponseResult<String> uploadFile(MultipartFile file, @PathVariable("id") String id) {
        try {
            // 判断订单是否存在
            Orders order = ordersService.getById(id);
            if (order == null) {
                return ResponseResult.fail(-1, "订单不存在");
            }
            // 此次录音序号
            int idx = order.getRecords() == null ? 0 : order.getRecords().split(",").length;
            // 上传文件路径
            String filePath = SpringUtils.getBean(DjApiProperties.class).getRecordUploadPath();
            // 上传并返回新文件名称
            String fileUrl = FileUploadUtils.uploadRecord(filePath, file, id, idx);
            // 更新订单录音路径
            order.setRecords(order.getRecords() == null ? fileUrl : order.getRecords() + "," + fileUrl);
            ordersService.updateById(order);
            // 返回
            return ResponseResult.success("音频上传成功", fileUrl);
        } catch (Exception e) {
            return ResponseResult.fail(e.getMessage());
        }
    }

}
